package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Storage;
import com.neuedu.service.StorageService;

@Controller
@RequestMapping("/storage")
public class StorageAction {

	@Autowired
	private StorageService storageservice;
	
	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startPage", (page - 1) * rows);
		pageMap.put("endPage", rows);

		List<Storage> u = storageservice.findAll(pageMap);
		int total = storageservice.countStorage();
		map.put("rows", u);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value = "/findList")
	public void findAll2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Storage> list = storageservice.findList();

		String str = JSONObject.toJSONString(list);
		response.getWriter().write(str);
	}
	
	@RequestMapping("add")
	public void addStorage(Storage storage,
				HttpServletRequest request,HttpServletResponse response) throws Exception{

		//调用业务逻辑层进行处理
		int res=storageservice.addStorage(storage);
		System.out.println(res);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
		
	}
	
	
	@RequestMapping(value="delete")
	public void deleteStorage(Integer storageNo,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("删除用户");
		int res=storageservice.deleteStorage(storageNo);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	
	@RequestMapping(value="update")
	public void updateStorage(Storage storage,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=storageservice.updateStorage(storage);
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
}
